package com.qf.service.impl;

import com.qf.config.RedisKey;
import com.qf.dao.repository.TeacherRepository;
import com.qf.pojo.dto.QfTeach;
import com.qf.pojo.resVo.BaseResult;
import com.qf.service.TeacherService;
import org.hibernate.annotations.common.util.impl.LoggerFactory;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherRepository teacherRepository;
    @Autowired
    RedisTemplate redisTemplate;

    //声明日志
    private static Logger logger  = LoggerFactory.logger(TeacherServiceImpl.class);

    @Override
    public BaseResult findAll() {
        //1.判断redis种是否有该key值
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.TEACHER_KEY);
        //2.如果返回为true 代表有该key值
        if (aBoolean){
            logger.debug("进入了redis进行查询");
            //3.从redis种获取到该value返回即可
            List range = redisTemplate.opsForList().range(RedisKey.TEACHER_KEY, 0, -1);
            return new BaseResult(1,"查询成功!",range,null);
        }else{
           logger.debug("进入了数据查询");
            //代表redis种没有该key，则需要从数据库先来进行查询。将结果集放置在Redis种
            List<QfTeach> all = teacherRepository.findAll();
            //将查询楚的结果集放置在redis种
            redisTemplate.opsForList().leftPushAll(RedisKey.TEACHER_KEY,all);
            return new BaseResult(1,"查询成功!",all,null);
        }
    }
}
